package com.example.yimai.controller;


import com.example.yimai.dto.PageResponseDTO;
import com.example.yimai.dto.UserLoginResponseDTO;
import com.example.yimai.entity.User;
import com.example.yimai.service.UserService;
import com.example.yimai.utils.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/login")
    public UserLoginResponseDTO login(@RequestParam String loginName,
                                      @RequestParam String password) {
        return userService.login(loginName, password);
    }

    /**
     * 用户注册
     * @param params 注册信息
     *               - loginName 用户名
     *               - userName 用户姓名
     *               - password 密码
     *               - email 邮箱
     *               - mobile 手机号码
     *               - verifyCode 验证码
     * @return
     */
    @RequestMapping("/register")
    public User register(@RequestParam Map<String, Object> params) {
        return userService.register(params);
    }


    @RequestMapping("/logout")
    public String logout(@RequestHeader("Authorization") String token){
        return userService.logout(token);
    }

    @GetMapping("/query")
    public PageResponseDTO<User> queryUser(@RequestParam(required = false) String keyword,
                                           @RequestParam(defaultValue = "1") int pageNum,
                                           @RequestParam(defaultValue = "10") int pageSize){
        return userService.queryUser(keyword, pageNum - 1, pageSize);
    }

    /**
     * 添加用户
     * @param params 添加参数
     *               - loginName 用户名
     *               - userName 用户姓名
     *               - password 密码
     *               - sex 性别
     *               - identityCode 身份证号码
     *               - email 邮箱
     *               - mobile 手机号码
     * @return
     */
    @PutMapping("/add")
    public String addUser(@RequestParam Map<String,Object> params){
        return userService.addUser(params);
    }



    @RequestMapping("/delete/{id}")
    public String deleteUser(@PathVariable Integer id){
        return userService.deleteUser(id);
    }

    /**
     * 修改用户信息
     * @param params 修改参数
     *               - id 用户ID(必要参数)
     *               - userName 用户姓名
     *               - password 密码
     *               - sex 性别
     *               - email 邮箱
     *               - identityCode 身份证号码
     *               - mobile 手机号码
     * @return
     */
    @RequestMapping("/update")
    public String updateUser(@RequestParam Map<String,Object> params){
        return userService.updateUser(params);
    }

    /**
     * 验证原密码是否正确
     * @param params 验证参数
     *               - id 用户ID
     *               - oldPassword 原密码
     * @return
     */
    @PostMapping("/checkPassword")
    public boolean checkPassword(@RequestParam Map<String,Object> params){
        Integer userId = ParamUtil.parseParamsInt(params, "id");
        String oldPassword = (String) params.get("oldPassword");
        if (userId == null || oldPassword == null || oldPassword.isEmpty()) {
            return false;
        }
        return userService.checkPassword(userId, oldPassword);
    }

}
